<?php
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';
	
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoFriend.php';
	require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoUpdate.php';
	
	class FriendBus	{
		private $_userBus;
		private $_friendDao;
		private $_updateDao;
		
		public function __construct() {
			$this->_userBus = new UserBus();
			$this->_friendDao = new FriendDao();
			$this->_updateDao = new UpdateDao();
		}
		
		public function addFriend($friend) {
			global $ubi;
			
			$id = $this->_friendDao->addFriend($friend);
			
			$update = new UpdateModel();
			$update->setIndicteeID($friend->getUserID());
			$update->setModifiedDate($_SERVER['REQUEST_TIME']);
			$update->setUpdateType($ubi['UPDATE']['SEND_REQUEST_MAKE_FRIEND']);
			$update->setUserID($friend->getFriendID());
			$update->setLink('index.php?view&type=user&userid=' . $friend->getUserID());
			$update->setName($this->_userBus->getUser($friend->getUserID())->getUserName());
			
			$this->_updateDao->addUpdate($update);
			
			if ($id) {
				global $ubi_score;
				$this->_userBus->addScore($friend->getUserID(), $ubi_score['MAKE_FRIEND']);
			}
			return $id;
		}
		
		public function getFriendsByUserId($userID) {
			return $this->_friendDao->getFriendsByUserId($userID);
		}
		
		public function getFriendsByFriendId($friendID) {
			return $this->_friendDao->getFriendsByFriendId($friendID);
		}
		
		public function isFriend($userID, $friendID) {
			return $this->_friendDao->isFriend($userID, $friendID);
		}
		
		public function updateFriendAccept($userID, $friendID) {
			global $ubi;
			
			$ck = $this->_friendDao->updateFriendAccept($userID, $friendID);
			if ($ck) {
				$update = new UpdateModel();
				$update->setIndicteeID($friendID);
				$update->setModifiedDate($_SERVER['REQUEST_TIME']);
				$update->setUpdateType($ubi['UPDATE']['SEND_REQUEST_MAKE_FRIEND']);
				$update->setUserID($userID);
				$update->setLink('index.php?view&type=user&userid=' . $friendID);
				$update->setName($this->_userBus->getUser($friendID)->getUserName());
				
				$this->_updateDao->addUpdate($update);
			}
			return $ck;
		}
		
		public function isExist($userID, $friendID) {
			return $this->_friendDao->isExist($userID, $friendID);
		}
		
		public function getStatus($userID, $friendID) { 
			$status = $this->_friendDao->getStatus($userID, $friendID);
			if (!isset($status)) {
				$friendStatus = $this->_friendDao->getStatus($friendID, $userID);
				if (isset($friendStatus)) {
					$status = 2;
				}
			}
			return $status;
		}
		
		public function getNumberOfFriend($userID) {
			return $this->_friendDao->getNumberOfFriend($userID);
		}
		
		public function getFriendsByUserIdOfPage($userID, $page) {
			global $ubi;
			
			$from = ($page - 1) * $ubi['SITE']['N_USER_PER_PAGE'];
			$to = $from +  $ubi['SITE']['N_USER_PER_PAGE']; 
			
			$friendList = $this->_friendDao->getFriendsOfUserFromTo($userID, $from, $to);
			if ($friendList) {
				$friendIdList = array();
				foreach ($friendList as $friend) {
					$friendIdList[] = $friend->getFriendID();
				}
				$userList = $this->_userBus->getUserListByUserIdList($friendIdList);
				
				return $userList;
			}
		}
		
		public function removeFriend($userID, $friendID) {
			return $this->_friendDao->removeFriend($userID, $friendID);
		}
	}
?>